<?
function setWebSiteLayout($layout){    
    if($layout == ""){
        return false;
    }
    
    $templatesDir = REAL_PATH."/template/";
    $layout = "$layout.html";    
    if(file_exists($templatesDir.$layout)){        
        $tpl = XTemplate::getInstance();
        $tpl->restart($layout, $templatesDir);
    } else {        
         return false;
    }
}

/**
 *  Redirecteaza catre un url
 * @param string $url
 */
function redirectToUrl($url){
    
    if($url != ""){        
        header("Location: $url");
        exit;
    }
}
/**
 * Incarca un module pentru a fi administrat
 * @param string $module 
 */
function includeModuleAdmin($module){
    if($module == ""){
        throw  new Exception('Numele modulului nu este corect');
    }
    $modulePath = "modules/$module";  
    if(is_dir($modulePath)){
        if(file_exists("$modulePath/admin.php")){
            if(file_exists("$modulePath/defines.php")){
                require_once "$modulePath/defines.php";
            }
            if(file_exists("$modulePath/common.php")){
                require_once "$modulePath/common.php";
            }
            if(file_exists("$modulePath/includes.php")){
                require_once "$modulePath/includes.php";
            }
            require_once "$modulePath/admin.php";

        } else {
            throw  new Exception("Modulul $module nu are un fisier admin.php");
        }
    } else {
        throw  new Exception("Modulul $module nu exista",1);

    }

}


/**
 * include un modul
 * - cod eroare 1: nu exista un director pentru acest modul
 * @param string $modul
 * @return true - daca modulul a fost incarcat cu succes, false altfel
 */
function includeModule($module){
    if($module == ""){
        throw  new Exception('Numele modulului nu este corect');
    }
    $modulePath = "modules/$module";    
    if(is_dir($modulePath)){        
        if(file_exists("$modulePath/index.php")){
            if(file_exists("$modulePath/defines.php")){
                require_once "$modulePath/defines.php";
            }
            if(file_exists("$modulePath/common.php")){
                require_once "$modulePath/common.php";
            }
            if(file_exists("$modulePath/includes.php")){
                require_once "$modulePath/includes.php";
            }
         
           
        } else {
            throw  new Exception("Modulul $module nu are un fisier index.php");
        }
    } else {
        throw  new Exception("Modulul $module nu exista",1);

    }
    
}

/**
 * porneste un modul
 * @param string $module
 */
function runModule($module){
    
    if($module == ""){
        throw  new Exception('Numele modulului nu este corect');
    }
   $query = "SELECT * FROM `modules` WHERE moduleName LIKE '{$module}' LIMIT 1";
   $result = mysql_query($query);
    if(mysql_num_rows($result) != 1){
        throw  new Exception("Modulul <b>$module</b> nu se afla in baza de date");
    }
    try {
        includeModule($module);
        $modulePath = "modules/$module";    
        require_once "$modulePath/index.php";
        $action = getCurrentRequest();
        if(trim($action) == ''){
            $action = 'default';
        }
        $function = $module.'_'.ucfirst($action);        
        if(function_exists($function)){
            call_user_func($function);
        }
    } catch (Exception $e){
        //cod 1: nu exista un director cu numele modulului
        if($e->getCode() != 1){
            throw  $e;
        } else {
            includeModule('static_content');
            if(function_exists('execute')){
                call_user_func('execute', 'action');
            }
            //modulul exista doar in baza de date
            //$moduleData = mysql_fetch_assoc($result);
            displayStaticModuleContent($moduleData);
        }
    }
}

/**
 * afiseaza un modul cu continut static
 * @param array module - datele modulului
 * 
 */
function displayStaticModuleContent(&$moduleData){    
    $tpl = XTemplate::getInstance();    
    $tpl->assign_file('CONTENT','static_module.html');
    $tpl->assign('moduleContent', $moduleData['moduleContent']);
    $tpl->assign('moduleName', $moduleData['moduleName']);
    $tpl->assign('metaTitle', $moduleData['moduleTitle']);
    $tpl->parse('main.metaTitle');
    $tpl->assign('metaKeywords', $moduleData['moduleMetaKeywords']);
    $tpl->parse('main.metaKeywords');
    $tpl->assign('metaDescription', $moduleData['moduleMetaDescription']);
    $tpl->parse('main.metaDesccription');
    $tpl->assign('moduleTitle',$moduleData['moduleTitle']);
    $tpl->parse('main.module_title');

}

/*
 * functii legate de module
 */
function getCurrentModule () {
    global $CURRENT_MODULE;

    if (!isset($CURRENT_MODULE) || $CURRENT_MODULE == '') {
        return false;
    }

    return $CURRENT_MODULE;
}

function setCurrentModule ($getModule = '') {
    global $CURRENT_MODULE;
       $module = "";
        if($getModule != ''){
            $getModule = mysql_real_escape_string($getModule);            
            $query = "SELECT * FROM `modules` WHERE moduleName LIKE '{$getModule}' LIMIT 1";
          
            $result = mysql_query($query);
         
            if(mysql_num_rows($result) == 1){
                $moduleData = mysql_fetch_assoc($result);
                if(trim($moduleData['moduleName']) != ""){                    
                    $CURRENT_MODULE = $moduleData['moduleName'];
                    $module = $moduleData['moduleName'];
                }
            }
        }
            if($module == '' && $getModule != ""){
                $query = "SELECT  * FROM `content` WHERE moduleUrl LIKE '/{$getModule}/' LIMIT 1";
                $result = mysql_query($query);               
                if(mysql_num_rows($result) == 1){
                    $moduleData = mysql_fetch_assoc($result);
                    $CURRENT_MODULE = $moduleData['moduleName'];
                    $module = $moduleData['moduleName'];
                }
            }
        
    if ($module == '') {
        $module = 'index';
    }
    $CURRENT_MODULE = $module;
}

function getCurrentRequest () {
    global $CURRENT_REQUEST;

    return $CURRENT_REQUEST;
}

function setCurrentRequest ($request = 'default') {
    global $CURRENT_REQUEST;

    $CURRENT_REQUEST = $request;
}

function getCurrentRequestParameters ($var = '') {
    global $CURRENT_REQUEST_PARAMETERS;
    if ($var != '') {
        if (!isset($CURRENT_REQUEST_PARAMETERS[$var])) {
            return '';
        }
        return $CURRENT_REQUEST_PARAMETERS[$var];
    }
    return $CURRENT_REQUEST_PARAMETERS;
}

function setCurrentRequestParameters ($var, $value) {
    global $CURRENT_REQUEST_PARAMETERS;

    if (!isset($CURRENT_REQUEST_PARAMETERS)) {
        $CURRENT_REQUEST_PARAMETERS = array();
    }
    $CURRENT_REQUEST_PARAMETERS[$var] = $value;
}

function createUrl($str){
    if(trim($str) == ''){
        return false;
    }
    $str = str_replace(' ', '-', $str);
    $str = preg_replace('#([^a-zA-Z0-9-_])#', '_', $str);
    return $str;
}

?>
